#define GLEW_STATIC
#include "GL\glew.h"
#include "GL\wglew.h"
#define FREEGLUT_STATIC
#include "GL\freeglut.h"
#include "GLTools.h"
#include "GLShaderManager.h"
#include "StopWatch.h"
#include <cstdio>

GLBatch triangleBatch;
GLBatch triangleBatch2;
GLShaderManager shaderManager;
CStopWatch watch;
float time;
char buffer[256];

void ChangeSize(int w, int h)
{
	glViewport(0, 0, w, h);
}
void SetupRC()
{
	// Blue background
	glClearColor(1.0f, 1.0f, 1.0f, 1.0f );
	glEnable(GL_CULL_FACE);
	glEnable(GL_DEPTH_TEST);
	shaderManager.InitializeStockShaders();
	// Load up a triangle
	GLfloat vVerts[] = { 0.0f, 0.5f ,0.9f,
		-0.5f, 0.0f , 0.9f,
		 0.5f, 0.0f, 0.9f};
	GLfloat vVerts2[] = {0.2f, 0.5f , 0.0f,
		-0.3f, 0.0f ,0.0f,
		0.7f, 0.0f, 0.0f };
	
	triangleBatch.Begin(GL_TRIANGLES, 3);
	triangleBatch.CopyVertexData3f(vVerts);
	triangleBatch.End();
	triangleBatch2.Begin(GL_TRIANGLES, 3);
	triangleBatch2.CopyVertexData3f(vVerts2);
	triangleBatch2.End();
}

void RenderScene(void)
{
	// Clear the window with current clearing color
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
	GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
	GLfloat vBlue[] = { 0.0f, 0.0f, 1.0f, 1.0f };

	
	shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vBlue);
	triangleBatch2.Draw();
	
	shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
	triangleBatch.Draw();
	
	// Perform the buffer swap to display the back buffer
	glutSwapBuffers();
}
int main(int argc, char* argv[])
{
	gltSetWorkingDirectory(argv[0]);
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
	glutInitWindowSize(800, 800);
	glutCreateWindow("Triangle of death");
	glutReshapeFunc(ChangeSize);
	glutDisplayFunc(RenderScene);
	GLenum err = glewInit();
	if (GLEW_OK != err) {
		fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
		return 1;
	}
	SetupRC();
	glutMainLoop();
	return 0;
}